import { fileURLToPath, URL } from "node:url";
import { resolve } from "path";
import { ConfigEnv, defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueDevTools from "vite-plugin-vue-devtools";

const envDir = "./"; //默认.env的路径 放在src下设置./src

export default defineConfig((config: ConfigEnv) => {
  const env = loadEnv(config.mode, envDir);

  return {
    envDir,
    plugins: [
      vue(),
      // vueDevTools(),
    ],
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import (reference) "${resolve(
              "src/theme/index.less"
            )}";`,
          },
          javascriptEnabled: true,
        },
      },
    },
    base: env.VITE_PUBLIC_PATH, //配置打包路由前缀
    server: {
      host: "0.0.0.0", //配置内网访问，
      port: env.VITE_PORT as unknown as number,
      open: JSON.parse(env.VITE_OPEN), //配置自动打开浏览器
      hmr: true,
      proxy: {
        "/api": {
          target: env.VITE_PROXY_URL,
          changeOrigin: true,
          rewrite: (path) => {
            return path.replace(/^\/api/, "");
          },
        },
      },
    },
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
  };
});
